package net.huashitong.supervision.task.util;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/9/16
 * Time: 20:57
 * To change this template use File | Settings | File Templates.
 * Description:
 */

import com.sirdc.modules.utils.ObjectUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *  * 获取某一时间段特定星期几的日期
 *  * @author finder.zhou
 *  
 */
public class WeekDayUtil {
    public static void main(String[] args) throws ParseException {
        getDates("2019-09-16", "2019-09-22", "7");
        Map<String, String> nextWeek = getNextWeek("2019-09-28");
        System.out.println(nextWeek.get("beginDateNext"));
        System.out.println(nextWeek.get("endDateNext"));
        int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        System.out.println(day);
        System.out.println(Integer.parseInt("01"));
        System.out.println(2019 + "-" + 01 + "-" + 03);
        System.out.println(isValidDate("2019-09-31"));
        List<String> l=new ArrayList<>();
        l.add("aaa");
        List<String> aaa = list(l, "aaa");
        System.out.println(aaa);
        System.out.println(getPastDate(7));
    }

    public static List<String> list(List<String> l,String s){
        boolean remove = l.remove(s);
        System.out.println(remove);
        boolean notBlank = ObjectUtils.isNotBlank(l);
        System.out.println("aaa"+l.size());
        return l;
    }
    /**
     * 获取某一时间段特定星期几的日期
     *
     * @param dateFrom 开始时间
     * @param dateEnd  结束时间
     * @param weekDays 星期
     * @return 返回时间数组
     */
    public static String[] getDates(String dateFrom, String dateEnd, String weekDays) {
        long time = 1l;
        long perDayMilSec = 24 * 60 * 60 * 1000;
        List<String> dateList = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//需要查询的星期系数
//        String strWeekNumber = weekForNum(weekDays);

        String strWeekNumber = Integer.parseInt(weekDays) + 1 + "";
        if ("7".equals(weekDays)) {
            strWeekNumber = "1";
        }
        try {
            dateFrom = sdf.format(sdf.parse(dateFrom).getTime() - perDayMilSec);
            while (true) {
                time = sdf.parse(dateFrom).getTime();
                time = time + perDayMilSec;
                Date date = new Date(time);
                dateFrom = sdf.format(date);
                if (dateFrom.compareTo(dateEnd) <= 0) {
//查询的某一时间的星期系数
                    Integer weekDay = dayForWeek(date);
//判断当期日期的星期系数是否是需要查询的
                    if (strWeekNumber.indexOf(weekDay.toString()) != -1) {
                        System.out.println(dateFrom);
                        dateList.add(dateFrom);
                    }
                } else {
                    break;
                }
            }
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        String[] dateArray = new String[dateList.size()];
        dateList.toArray(dateArray);
        return dateArray;
    }

    //等到当期时间的周系数。星期日：1，星期一：2，星期二：3，星期三：4，星期四：5，星期五：6，星期六：7


    public static Integer dayForWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }

    /**
     *      * 得到对应星期的系数  星期日：1，星期一：2，星期二：3，星期三：4，星期四：5，星期五：6，星期六：7
     *      * @param weekDays 星期格式  星期一|星期二
     *      
     */

    public static String weekForNum(String weekDays) {
        //返回结果为组合的星期系数
        String weekNumber = "";
        //解析传入的星期
        if (weekDays.indexOf("|") != -1) {//多个星期数
            String[] strWeeks = weekDays.split("\\|");
            for (int i = 0; i < strWeeks.length; i++) {
                weekNumber = weekNumber + "" + getWeekNum(strWeeks[i]).toString();
            }
        } else {//一个星期数
            weekNumber = getWeekNum(weekDays).toString();
        }

        return weekNumber;

    }


    public static Integer getWeekNum(String strWeek) {
        Integer number = 1;//默认为星期日
        if ("星期日".equals(strWeek)) {
            number = 1;
        } else if ("星期一".equals(strWeek)) {
            number = 2;
        } else if ("星期二".equals(strWeek)) {
            number = 3;
        } else if ("星期三".equals(strWeek)) {
            number = 4;
        } else if ("星期四".equals(strWeek)) {
            number = 5;
        } else if ("星期五".equals(strWeek)) {
            number = 6;
        } else if ("星期六".equals(strWeek)) {
            number = 7;
        } else {
            number = 1;
        }
        return number;
    }

    /**
     * 当前时间所在一周的周一和周日时间
     *
     * @return
     */
    public static Map<String, String> getWeekDate() {
        Map<String, String> map = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (dayWeek == 1) {
            dayWeek = 8;
        }
        System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期

        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        Date mondayDate = cal.getTime();
        String weekBegin = sdf.format(mondayDate);
        System.out.println("所在周星期一的日期：" + weekBegin);


        cal.add(Calendar.DATE, 4 + cal.getFirstDayOfWeek());
        Date sundayDate = cal.getTime();
        String weekEnd = sdf.format(sundayDate);
        System.out.println("所在周星期日的日期：" + weekEnd);

        map.put("mondayDate", weekBegin);
        map.put("sundayDate", weekEnd);
        return map;
    }

    /**
     * 获取下周一周日
     *
     * @param queryDate
     * @return
     * @throws ParseException
     */
    public static Map<String, String> getNextWeek(String queryDate) throws
            ParseException {

        Map<String, String> dateMap = new HashMap<String, String>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar cal1 = Calendar.getInstance();

        Calendar cal2 = Calendar.getInstance();

        cal1.setTime(sdf.parse(queryDate));

        cal2.setTime(sdf.parse(queryDate));

        int dayWeek = cal1.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天

        if (dayWeek == 1) {

            cal1.add(Calendar.DAY_OF_MONTH, 1);

            cal2.add(Calendar.DAY_OF_MONTH, 7);
        } else {

            cal1.add(Calendar.DAY_OF_MONTH, 1 - dayWeek + 8);

            cal2.add(Calendar.DAY_OF_MONTH, 1 - dayWeek + 14);

        }

        // 下周 周一日期

        String beginDateNext = sdf.format(cal1.getTime());

        // 下周 周日日期

        String endDateNext = sdf.format(cal2.getTime());

        dateMap.put("beginDateNext", beginDateNext);

        dateMap.put("endDateNext", endDateNext);

        return dateMap;
    }

    public static boolean isValidDate(String str) {
        boolean convertSuccess = true;
        // 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            format.parse(str);
            System.out.println(str);
        } catch (ParseException e) {
            // e.printStackTrace();
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            convertSuccess = false;
        }
        return convertSuccess;
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
//        Log.e(null, result);
        return result;
    }
}
